package bg.cinema_system.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import bg.cinema_system.global.ConnectionJDBC;

public class ProjectionDAO {

	public static int getHallID(int projection_id) throws SQLException {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ProjectionDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT projections.fk_hall_id FROM cinema.projections " 
		+ "WHERE projections.projection_id = ?";

		PreparedStatement ps = dbConnection.prepareStatement(sqlStatement);
		ps.setInt(1, projection_id);

		// Get customer data from database
		ResultSet result = ps.executeQuery();

		int hall_id = -1;

		while (result.next()) {
			hall_id = result.getInt("fk_hall_id");
		}

		return hall_id;
	}
	
	public static int getMovieID(int projection_id) throws SQLException {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ProjectionDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT projections.fk_movie_id FROM cinema.projections " 
		+ "WHERE projections.projection_id = ?";

		PreparedStatement ps = dbConnection.prepareStatement(sqlStatement);
		ps.setInt(1, projection_id);

		// Get customer data from database
		ResultSet result = ps.executeQuery();

		int movie_id = -1;

		while (result.next()) {
			movie_id = result.getInt("fk_movie_id");
		}

		return movie_id;
	}
	
	public static Date getStartTime(int projection_id) throws SQLException {
		
		Connection dbConnection = ConnectionJDBC.getConnection();

		if (dbConnection == null) {
			throw new SQLException("Class ProjectionDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT projections.projection_start FROM cinema.projections " 
		+ "WHERE projections.projection_id = ?";

		PreparedStatement ps = dbConnection.prepareStatement(sqlStatement);
		ps.setInt(1, projection_id);

		// Get customer data from database
		ResultSet result = ps.executeQuery();

		Date projection_start = null;

		while (result.next()) {
			projection_start = new Date(result.getTimestamp("projection_start").getTime());
		}

		return projection_start;
	}
}
